﻿Imports System.Windows.Forms

Public Class QuizDetails

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If ImageOpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            ImageTextBox.Text = ImageOpenFileDialog.FileName
        End If
    End Sub

    Private Sub ImageTextBox_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ImageTextBox.DragDrop
        ImageTextBox.Text = e.Data.GetData(GetType(String))
    End Sub

    Private Sub ImageTextBox_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ImageTextBox.DragEnter
        e.Effect = DragDropEffects.Copy
    End Sub

    Private Sub ImageTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImageTextBox.TextChanged
        Cursor = Cursors.WaitCursor
        Dim image As Image = Nothing
        Try
            image = image.FromFile(ImageTextBox.Text)
        Catch
            If ImageTextBox.Text.StartsWith("http://", True, Globalization.CultureInfo.CurrentCulture) Or ImageTextBox.Text.StartsWith("ftp://", True, Globalization.CultureInfo.CurrentCulture) Then
                Dim WC As New Net.WebClient
                Try
                    image = image.FromStream(WC.OpenRead(ImageTextBox.Text))
                Catch
                    Dim creds As New Net.NetworkCredential
                    creds.UserName = InputBox("Proxy User Name Required")
                    creds.Password = InputBox("Proxy Password Required")
                    WC.Proxy.Credentials = creds
                End Try
            Else : image = Nothing
            End If
        End Try
        PictureBox1.Image = image
        Try
            Dim ms As New IO.MemoryStream
            image.Save(ms, Imaging.ImageFormat.Gif)
            CType(QuizInfoBindingSource.Current.row, Database.QuizInfoRow).QuizImage = Convert.ToBase64String(ms.ToArray)
            ms.Close()
        Catch
        End Try
        Cursor = Cursors.Default
    End Sub

    Private Sub RemoveImageButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveImageButton.Click
        CType(QuizInfoBindingSource.Current.row, Database.QuizInfoRow).SetQuizImageNull()
        PictureBox1.Image = Nothing
    End Sub

    Private Sub QuizDetails_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If QuizInfoBindingSource.Count = 0 Then
            QuizInfoBindingSource.AddNew()
        Else
            QuizInfoBindingSource.Position = 0
        End If
    End Sub

    Public Sub UpdateBinding()
        QuizInfoBindingSource.ResetBindings(False)
        If QuizInfoBindingSource.Count = 0 Then
            QuizInfoBindingSource.AddNew()
        Else
            QuizInfoBindingSource.Position = 0
        End If
    End Sub

    Private Sub PasteImageFromClipboardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteImageFromClipboardToolStripMenuItem.Click
        Cursor = Cursors.WaitCursor
        Dim Image As Image = My.Computer.Clipboard.GetImage()
        PictureBox1.Image = Image
        Try
            Dim ms As New IO.MemoryStream
            Image.Save(ms, Imaging.ImageFormat.Gif)
            CType(QuizInfoBindingSource.Current.row, Database.QuizInfoRow).QuizImage = Convert.ToBase64String(ms.ToArray)
            ms.Close()
        Catch
        End Try
        Cursor = Cursors.Default
    End Sub

    Private Sub QuizInfoBindingSource_CurrentItemChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles QuizInfoBindingSource.CurrentItemChanged
        Try
            Dim ms As New IO.MemoryStream(Convert.FromBase64String(CType(QuizInfoBindingSource.Current.row, Database.QuizInfoRow).QuizImage))
            PictureBox1.Image = Image.FromStream(ms)
        Catch
            PictureBox1.Image = Nothing
        End Try
    End Sub
End Class
